package com.biaobiaoqi.scoreanalysis.administrator.action;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.regexp.recompile;
import org.apache.struts2.interceptor.SessionAware;

import com.biaobiaoqi.scoreanalysis.exception.AuthenticateFailedException;
import com.biaobiaoqi.scoreanalysis.service.IAdministratorService;
import com.opensymphony.xwork2.ActionSupport;

public class SaveAdminPassword extends ActionSupport implements SessionAware{
	Log log = LogFactory.getLog(SaveAdminPassword.class);
 	/**
	 * 
	 */
	private static final long serialVersionUID = -4182484130965768377L;
  	private String originalPassword;
  	private String newPassword;
  	private String confirmPassword;
 	private Map<String, Object> session;


	@Resource
 	private IAdministratorService administratorService;

	public String execute() {
		log.debug("originalPassword:"+originalPassword+" newPassword:"+newPassword+" confirmPassword:"+confirmPassword);
		try {
			if (!newPassword.equals(confirmPassword)) {
				addActionError("两次密码输入不一致！");
				return INPUT;
			}
			Integer userId = (Integer) session.get("userId");
			administratorService.modifyAdministratorPassword(userId, originalPassword, confirmPassword);
			return SUCCESS;
		} catch (AuthenticateFailedException e) {
			addActionError("原密码错误！");
			return INPUT;
		} catch (Exception e) {
			log.error("save admin info failed.", e);
			addActionError("服务器内部错误");
			return ERROR;
		}
	}

	public IAdministratorService getAdministratorService() {
		return administratorService;
	}

	public void setAdministratorService(
			IAdministratorService administratorService) {
		this.administratorService = administratorService;
	}

 
 
 	@Override
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}

	public String getOriginalPassword() {
		return originalPassword;
	}

	public void setOriginalPassword(String originalPassword) {
		this.originalPassword = originalPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public Map<String, Object> getSession() {
		return session;
	}
}
